package com.atguigu.ssyx.acl.controller;

import com.atguigu.ssyx.acl.service.AdminService;
import com.atguigu.ssyx.acl.service.RoleService;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.acl.Admin;
import com.atguigu.ssyx.vo.acl.AdminQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Controller
@ResponseBody
@RequestMapping("/admin/acl/user")
@Api(tags = "用户接口")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private RoleService roleService;

    //1.用户列表
    @GetMapping("{page}/{limit}")
    @ApiOperation(value = "获得用户分页列表")
    public Result getPageList(@PathVariable Long page, @PathVariable Long limit, @ApiParam(name = "adminQueryVo", value = "附加信息") AdminQueryVo adminQueryVo) {
        Page<Admin> pageParams = new Page<>(page, limit);
        IPage<Admin> pageModel = adminService.selectPage(pageParams, adminQueryVo);
        return Result.ok(pageModel);
    }


//    2.id查询用户

    @ApiOperation(value = "获取管理用户")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        Admin user = adminService.getById(id);
        return Result.ok(user);
    }


    @ApiOperation(value = "新增管理员")
    @PostMapping("save")
    public Result save(@RequestBody Admin user) throws NoSuchAlgorithmException {
        //对密码进行MD5处理
        adminService.save(user);
        return Result.ok(user);
    }

    @ApiOperation(value = "修改管理用户")
    @PutMapping("update")
    public Result updateById(@RequestBody Admin admin) {
        adminService.updateById(admin);
        return Result.ok(null);
    }


    @ApiOperation(value = "根据id列表删除管理用户")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        adminService.removeByIds(idList);
        return Result.ok(null);
    }

    @ApiOperation(value = "删除管理用户")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        adminService.removeById(id);
        return Result.ok(null);
    }


    @ApiOperation("获取某个用户的所有角色")
    @GetMapping("toAssign/{adminId}")
    public Result getAdmin(@PathVariable Long adminId) {
//        返回map集合包括两个部分,所有角色和为用户分配角色列表.
        Map<String, Object> map = roleService.getRoleByAdminId(adminId);
        return Result.ok(map);
    }

    @ApiOperation("为用户进行分配角色")
    @PostMapping("doAssign")
    public Result assignRoles(@RequestParam Long adminId, @RequestParam Long[] roleId) {
        roleService.saveUserRoleRealtionShip(adminId, roleId);
        return Result.ok(null);
    }


}
